package com.ruoyi.system.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Parent;
import com.ruoyi.system.mapper.ParentMapper;
import com.ruoyi.system.mapper.SchoolMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.utils.Upload;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StudentMapper;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.service.IStudentService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 学生Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-25
 */
@Service
@Slf4j
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Resource
    private SchoolMapper schoolMapper;

    @Resource
    private ParentMapper parentMapper;

    @Resource
    private SysDeptServiceImpl sysDeptService;

    /**
     * 查询学生
     *
     * @param studentId 学生主键
     * @return 学生
     */
    @Override
    public Student selectStudentByStudentId(Long studentId) {
        return studentMapper.selectStudentByStudentId(studentId);
    }

    /**
     * 查询学生列表
     *
     * @param student 学生
     * @return 学生
     */
    @Override
    public List<Student> selectStudentList(Student student) {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生
     *
     * @param student 学生
     * @return 结果
     */
    @Override
    public int insertStudent(Student student) {
        student.setCreateTime(DateUtils.getNowDate());
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生
     *
     * @param student 学生
     * @return 结果
     */
    @Override
    public int updateStudent(Student student) {
        student.setUpdateTime(DateUtils.getNowDate());
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生
     *
     * @param studentIds 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentByStudentIds(Long[] studentIds) {
        return studentMapper.deleteStudentByStudentIds(studentIds);
    }

    /**
     * 删除学生信息
     *
     * @param studentId 学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentByStudentId(Long studentId) {
        return studentMapper.deleteStudentByStudentId(studentId);
    }

    /**
     * 学生信息批量上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public int uploadAndUpdate(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("拿到文件后缀 = " + suffix);
        if (!suffix.equals(".xlsx")) {
            log.error("上传文件失败,请检查格式,文件需为.xlsx类型");
            return 4;
        }

        XSSFWorkbook wb1 = new XSSFWorkbook(file.getInputStream());

        String[][] result = new String[0][];
        try {
            // 获取Excel表文件里的数据
            //第二个参数为从Excel文件里第几行开始读
            result = Upload.getData(wb1, 1);
            for (int i = 0; i < result.length; i++) {
                for (int j = 0; j < result[i].length - 1; j++) {
                    log.info("result[i][j] = " + "result[" + i + "][" + j + "] = " + result[i][j]);
                }
            }
            int len = result.length;
            log.info("result.length = " + len);
//            log.info("result[0][1] = " + result[0][1]);
            if (len < 0) {
                return 2;
            }
            Integer sidToLoginUser = sysDeptService.getSidToLoginUser();
            log.info("sid = " + sidToLoginUser);
            //从result[i][12]开始获取家长信息
            for (int i = 0; i < result.length; i++) {
                Student student = new Student();
                student.setSid(Long.valueOf(sidToLoginUser));
                student.setStudentName(result[i][0]);
                student.setStudentSex(result[i][1]);
                student.setStudentAge(result[i][2]);
                student.setDateOfBirth(result[i][3]);
                student.setNation(result[i][4]);
                Student student1 = studentMapper.selectRepeatCardNumber(result[i][5]);
                if (student1 != null) {
                    return 3;
                }
                student.setCardNumber(result[i][5]);
                student.setStudentStatus(result[i][6]);
                student.setStudentAddress(result[i][7]);
                student.setStudentPhone(result[i][8]);
                int grade = 0;
                switch (result[i][9]) {
                    case "小学一年级":
                        grade = 1;
                        break;
                    case "小学二年级":
                        grade = 2;
                        break;
                    case "小学三年级":
                        grade = 3;
                        break;
                    case "小学四年级":
                        grade = 4;
                        break;
                    case "小学五年级":
                        grade = 5;
                        break;
                    case "小学六年级":
                        grade = 6;
                        break;
                    case "初一":
                        grade = 7;
                        break;
                    case "初二":
                        grade = 8;
                        break;
                    case "初三":
                        grade = 9;
                        break;
                    case "高一":
                        grade = 10;
                        break;
                    case "高二":
                        grade = 11;
                        break;
                    case "高三":
                        grade = 12;
                        break;
                    case "大一":
                        grade = 13;
                        break;
                    case "大二":
                        grade = 14;
                        break;
                    case "大三":
                        grade = 15;
                        break;
                    case "大四":
                        grade = 16;
                        break;
                    case "小升初":
                        grade = 17;
                        break;
                    default:
                        grade = 18;
                        break;
                }
                student.setStudentBelongToGrade((long) grade);
                student.setStudentBelongToClass(Long.valueOf(result[i][10]));
                if (result[i][11].equals("文科")) {
                    student.setDivision(1L);
                } else if (result[i][11].equals("理科")) {
                    student.setDivision(2L);
                } else {
                    student.setDivision(0L);
                }
                student.setFraction(String.valueOf(0));
                student.setStatus(1L);
                student.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                student.setCreateTime(new Date());
                student.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
                student.setUpdateTime(new Date());
                student.setIsDelete(1L);
                studentMapper.insertStudent(student);
                log.info("批量添加第" + (i + 1) + "条学生信息完成");
                //添加对应家长信息
                Parent parent = new Parent();
                Student student2 = studentMapper.selectStudentIdToNameAndSid(sidToLoginUser, result[i][0], result[i][5]);
                parent.setStudentId(student2.getStudentId());
                parent.setParentName(result[i][12]);
                parent.setRelationship(result[i][13]);
                parent.setParentJob(result[i][14]);
                parent.setParentPhone(result[i][15]);
                parent.setParentAddress(result[i][16]);
                parent.setStatus(1L);
                parent.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                parent.setCreateTime(new Date());
                parent.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
                parent.setUpdateTime(new Date());
                parent.setIsDelete(1L);
                parentMapper.insertParent(parent);
                log.info("批量添加第" + (i + 1) + "条家长信息完成");
            }
            return 0;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            wb1.close();
        }
        return 1;
    }
}

























